package Gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import Robot.RobotControl;

public class DrivePolygonDialog {

	private static final Font FONT = new Font("Tahoma", Font.PLAIN, 12);
	private JFrame frame;
	private JDialog dialog;
	private JTextField polygonSides;
	private JTextField polygonLength;
	private RobotControl robotControl;
	private ActionListener radioButtonListener = new RadioButtonListener();
	private JFrame owner;
	
	/**
	 * Create the dialog.
	 */
	public DrivePolygonDialog(RobotControl control, JFrame owner) {
		this.robotControl = control;
		this.owner = owner;
		try {
			UIManager.setLookAndFeel(
			        UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
		} catch (InstantiationException e) {
		} catch (IllegalAccessException e) {
		} catch (UnsupportedLookAndFeelException e) {
		}
		initialize();
	}
	
	public void open() {
		dialog.setVisible(true);
	}

	private void initialize() {
		frame = new JFrame("Polygon");
		dialog = new JDialog(owner);
		dialog.setAlwaysOnTop(true);
		dialog.setBounds(100, 100, 230, 175);
		dialog.getContentPane().setLayout(new BorderLayout(0, 0));
		/*frame.setBounds(100, 100, 230, 175);
		frame.setAlwaysOnTop(true);
		frame.setResizable(false);
		frame.getRootPane().setWindowDecorationStyle(JRootPane.INFORMATION_DIALOG);
		frame.getContentPane().setLayout(new BorderLayout(0, 0));*/
		
		JLabel titleLabel = new JLabel("Properties Polygon");
		titleLabel.setFont(new Font("Tahoma", Font.BOLD, 12));
		titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
		dialog.getContentPane().add(titleLabel, BorderLayout.NORTH);
		
		JPanel panel = new JPanel();
		dialog.getContentPane().add(panel);
		panel.setLayout(null);
		
		JLabel lblSides = new JLabel("# sides:");
		lblSides.setBounds(30, 12, 43, 15);
		lblSides.setFont(FONT);
		panel.add(lblSides);
		
		polygonSides = new JTextField();
		polygonSides.setBounds(80, 11, 86, 20);
		polygonSides.setColumns(10);
		panel.add(polygonSides);
		
		JLabel lblLength = new JLabel("Length:");
		lblLength.setBounds(30, 40, 43, 15);
		lblLength.setFont(FONT);
		panel.add(lblLength);
		
		polygonLength = new JTextField();
		polygonLength.setBounds(80, 38, 86, 20);
		polygonLength.setColumns(10);
		panel.add(polygonLength);
		
		JLabel lblCm = new JLabel("cm");
		lblCm.setBounds(175, 40, 16, 15);
		lblCm.setFont(FONT);
		panel.add(lblCm);
		
		JRadioButton rdbtnLeft = new JRadioButton("Left");
		rdbtnLeft.setBounds(115, 65, 45, 23);
		panel.add(rdbtnLeft);
		
		JRadioButton rdbtnRight = new JRadioButton("Right");
		rdbtnRight.setBounds(45, 65, 51, 23);
		rdbtnRight.setSelected(true);
		panel.add(rdbtnRight);
		
		ButtonGroup polygonTurnDirection = new ButtonGroup();
		polygonTurnDirection.add(rdbtnLeft);
		polygonTurnDirection.add(rdbtnRight);
		
		JButton btnStart = new JButton("Start");
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				checkInput();
			}
		});
		btnStart.setBounds(30, 95, 57, 23);
		panel.add(btnStart);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dialog.setVisible(false);
			}
		});
		btnCancel.setBounds(126, 95, 65, 23);
		panel.add(btnCancel);
		
		rdbtnLeft.addActionListener(radioButtonListener);
		rdbtnRight.addActionListener(radioButtonListener);
	}
	
	private void checkInput() {
		int numberOfSides = 0;
		boolean sidesCorrect = false;
		int length = 0;
		boolean lengthCorrect = false;
		polygonSides.setBackground(Color.white);
		polygonLength.setBackground(Color.white);
		
		if (polygonSides.getText() != "") {
			try {
				numberOfSides = Integer.parseInt(polygonSides.getText());
				if (numberOfSides <= 0)
					polygonSides.setBackground(Color.red);
				else
					sidesCorrect = true;
			} catch (NumberFormatException ex) {
				polygonSides.setBackground(Color.red);
			}
		} else
			polygonSides.setBackground(Color.red);
		
		if (polygonLength.getText() != "") {
			try {
				length = Integer.parseInt(polygonLength.getText());
				if (length <= 0)
					polygonLength.setBackground(Color.red);
				else
					lengthCorrect = true;
			} catch (NumberFormatException ex) {
				polygonLength.setBackground(Color.red);
			}
		} else
			polygonLength.setBackground(Color.red);
		
		if (sidesCorrect && lengthCorrect) {
			robotControl.setPolygon(numberOfSides, length);
			robotControl.setDrivePolygon(true);
			
			dialog.setVisible(false);
		}	
	}
	
	private class RadioButtonListener implements ActionListener {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			String action = e.getActionCommand();
			if(action.equals("Left"))
				robotControl.setPolygonTurnDirection(false);
			if(action.equals("Right"))
				robotControl.setPolygonTurnDirection(true);
		}
		
	}
}
